package com.niiwoo.civet.trade.service.dubbo.refund;

import com.alibaba.dubbo.config.annotation.Service;
import com.niiwoo.civet.trade.dto.common.BatchTransOrderDetailForAccountDTO;
import com.niiwoo.civet.trade.dto.common.RefundCallBackForAccountDTO;
import com.niiwoo.civet.trade.dto.common.TradeBatchTransOrderDTO;
import com.niiwoo.civet.trade.dto.request.myloan.BorrowerProjectAppDTO;
import com.niiwoo.civet.trade.dto.request.myloan.ProjectRepaymentDTO;
import com.niiwoo.civet.trade.dto.response.myloan.BorrowerProjectAppResponseDTO;
import com.niiwoo.civet.trade.dto.response.myloan.ProjectrePaymentResponseDTO;
import com.niiwoo.civet.trade.dto.response.myloan.QueryRepaymentProjectSummaryDTO;
import com.niiwoo.civet.trade.dto.response.myloan.RefundGuideContentResponseDTO;
import com.niiwoo.civet.trade.service.local.newRefund.common.RefundLocalService;
import com.niiwoo.civet.trade.service.local.refund.RepaymentRemindLocalService;
import com.niiwoo.civet.trade.service.refund.RepaymentDubboService;
import lombok.extern.slf4j.Slf4j;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.Assert;

import java.util.List;
import java.util.Map;

/**
 * 还款相关
 * Created by bincun on 2017/11/8.
 */
@Slf4j
@Service(version = "1.0.0")
public class RepaymentDubboServiceImpl implements RepaymentDubboService {

	@Autowired
	private RefundLocalService refundLocalService;
	
	@Autowired
	private RepaymentRemindLocalService repaymentRemindLocalService;
	
    @Override
    public void repaymentBorrow(boolean isWithHold) {
    	log.info("---repaymentBorrow---");
    }

    @Override
    public void autoCutDate(){
    	log.info("---autoCutDate---");
    }

    @Override
    public void repaymentAutoRefundWithGuarantee() {
    	log.info("---repaymentAutoRefundWithGuarantee---");
    }

    @Override
    public void repaymentWithAutoGuarantee(String date, String userId) {
        log.info("---repaymentWithAutoGuarantee---");
    }

    @Override
    public void testGuarantee(String srcBatchNo) {
		log.info("---testGuarantee---", srcBatchNo);
    }

    @Override
    public BatchTransOrderDetailForAccountDTO generateDetailForNormal(TradeBatchTransOrderDTO dto){
        return null;
    }

    @Override
    public BatchTransOrderDetailForAccountDTO generateDetailForOverDebt(TradeBatchTransOrderDTO dto){
        return null;
    }

    @Override
    public void repaymentCallBackForAccountNew(List<RefundCallBackForAccountDTO> dto){
    }
    
    public BorrowerProjectAppResponseDTO appRepayment(BorrowerProjectAppDTO dto){
//        Assert.notNull(dto,"参数对象不可空");
//        Assert.notNull(dto.getProjectId(),"标的号不可空");
//        Assert.notNull(dto.getUserId(),"用户ID不可空");
//        log.info("APP还款,dto={}", JSON.toJSONString(dto));
//        if (!HintManagerHolder.isMasterRouteOnly()) {
//            log.info("appRepayment isMasterRouteOnly-projectId={}",dto.getProjectId());
//
//            HintManagerHolder.clear();
//            HintManager hintManager = HintManager.getInstance();
//            hintManager.setMasterRouteOnly();
//        }
//        return repaymentForAppLocalService.appRepayment(dto);
    	return null;
    }

    @Override
    public BorrowerProjectAppResponseDTO webRepayment(ProjectRepaymentDTO dto){
//        Assert.notNull(dto,"参数对象不可空");
//        Assert.notNull(dto.getProjectId(),"标的号不可空");
//        Assert.notNull(dto.getUserId(),"用户ID不可空");
//        log.info("APP还款,dto={}", JSON.toJSONString(dto));
//        return repaymentFacade.repayement(dto);
    	return null;
    }

    /**
     * 管理后台(还款)
     */
    public BorrowerProjectAppResponseDTO manageRepayment(BorrowerProjectAppDTO dto){
//        Assert.notNull(dto,"参数对象不可空");
//        Assert.notNull(dto.getProjectId(),"标的号不可空");
//        Assert.notNull(dto.getUserId(),"用户ID不可空");
//        Assert.notNull(dto.getType(),"还款类型不可空");
//        if (dto.getWithholdAmount().compareTo(BigDecimal.ZERO) < 0){
//            throw new IllegalArgumentException("手动指定代扣金额不能为负数");
//        }
//        log.info("后台代扣还款,dto={}", JSON.toJSONString(dto));
//        if (!HintManagerHolder.isMasterRouteOnly()) {
//            log.info("manageRepayment isMasterRouteOnly-projectId={}",dto.getProjectId());
//            HintManagerHolder.clear();
//            HintManager hintManager = HintManager.getInstance();
//            hintManager.setMasterRouteOnly();
//        }
//        return repaymentForManageLocalService.manageRepayment(dto);
    	return null;
    }

    @Override
    public QueryRepaymentProjectSummaryDTO getRepaymentProjectSummary(Long projectId) {
        return refundLocalService.getRepaymentProjectSummary(projectId);
    }

    @Override
    public List<ProjectrePaymentResponseDTO> getProjectRepaymentForProjectIdList(List<Map<String, Long>> borrowerUserIdProjectIdDTO) {
        Assert.notEmpty(borrowerUserIdProjectIdDTO,"参数对象不可空");
        return refundLocalService.getProjectRepaymentForProjectIdList(borrowerUserIdProjectIdDTO);
    }

	@Override
	public int queryRefundStatusByBatchRequestNo(String batchRequestNo) {
		return 0;
	}

    @Override
    public RefundGuideContentResponseDTO refundGuideContent(String userId) {
        return null;    
    }

    @Override
    public void checkRepaymentByProjectId(Long projectId){
		Assert.notNull(projectId, "标的ID不可空");
        refundLocalService.checkRepaymentByProjectId(projectId);
    }

    @Override
    public void updatePenaltyFeeForPlan(){
    	log.info("------------------updatePenaltyFeeForPlan------------------");
    }

    @Override
    public int updateTransOrderToFailed(String reqestNo){
    	log.info("------------------updateTransOrderToFailed------------------");
        return 0;
    }

    @Override
    public void updateTransOrderToFailedForJob(){
    	log.info("------------------updateTransOrderToFailedForJob------------------");    	
    }

    @Override
    public void sendRepaymentRemindMessageForJob(){
    	repaymentRemindLocalService.sendNormalRepaymentRemindMessage();
    }
    
    @Override
    public void sendRepaymentOverdueMessageForJob(){
    	repaymentRemindLocalService.sendOverdueRepaymentRemindMessage();
    }

    @Override
    public List<ProjectrePaymentResponseDTO> listRepaymentPlanByProjectIdAndUserId(Long projectId, String userId) {
        return refundLocalService.listRepaymentPlanByProjectIdAndUserId(projectId, userId);
    }

    @Override
    public Integer countOverdueProject(String userId){
        return refundLocalService.countOverdueProject(userId);
    }

    @Override
    public Integer selectOverdueExceedConditionRepayment(String userId, Integer exceedDays) {
        return refundLocalService.selectOverdueExceedConditionRepayment(userId, exceedDays);
    }
}